package com.jf.readers.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.jf.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.jf.readers.domain.BorrowRecords;
import com.jf.readers.mapper.ReadersMapper;
import com.jf.readers.domain.Readers;
import com.jf.readers.service.IReadersService;

/**
 * 读者基本信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-14
 */
@Service
public class ReadersServiceImpl implements IReadersService 
{
    @Autowired
    private ReadersMapper readersMapper;

    /**
     * 查询读者基本信息
     * 
     * @param readerId 读者基本信息主键
     * @return 读者基本信息
     */
    @Override
    public Readers selectReadersByReaderId(Long readerId)
    {
        return readersMapper.selectReadersByReaderId(readerId);
    }

    /**
     * 查询读者基本信息列表
     * 
     * @param readers 读者基本信息
     * @return 读者基本信息
     */
    @Override
    public List<Readers> selectReadersList(Readers readers)
    {
        return readersMapper.selectReadersList(readers);
    }

    /**
     * 新增读者基本信息
     * 
     * @param readers 读者基本信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertReaders(Readers readers)
    {
        int rows = readersMapper.insertReaders(readers);
        insertBorrowRecords(readers);
        return rows;
    }

    /**
     * 修改读者基本信息
     * 
     * @param readers 读者基本信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateReaders(Readers readers)
    {
        readersMapper.deleteBorrowRecordsByReaderId(readers.getReaderId());
        insertBorrowRecords(readers);
        return readersMapper.updateReaders(readers);
    }

    /**
     * 批量删除读者基本信息
     * 
     * @param readerIds 需要删除的读者基本信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteReadersByReaderIds(Long[] readerIds)
    {
        readersMapper.deleteBorrowRecordsByReaderIds(readerIds);
        return readersMapper.deleteReadersByReaderIds(readerIds);
    }

    /**
     * 删除读者基本信息信息
     * 
     * @param readerId 读者基本信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteReadersByReaderId(Long readerId)
    {
        readersMapper.deleteBorrowRecordsByReaderId(readerId);
        return readersMapper.deleteReadersByReaderId(readerId);
    }

    /**
     * 新增图书借阅记录信息
     * 
     * @param readers 读者基本信息对象
     */
    public void insertBorrowRecords(Readers readers)
    {
        List<BorrowRecords> borrowRecordsList = readers.getBorrowRecordsList();
        Long readerId = readers.getReaderId();
        if (StringUtils.isNotNull(borrowRecordsList))
        {
            List<BorrowRecords> list = new ArrayList<BorrowRecords>();
            for (BorrowRecords borrowRecords : borrowRecordsList)
            {
                borrowRecords.setReaderId(readerId);
                list.add(borrowRecords);
            }
            if (list.size() > 0)
            {
                readersMapper.batchBorrowRecords(list);
            }
        }
    }
}
